import java.util.Vector;

/**
 * Class holding the structure of a East/West Road, which extends Road.
 * @author Jared Lindbl�m
 */
public class EastWestRoad extends Road {
	private Vector<Vehicle> eastBound;
	private Vector<Vehicle> westBound;
	private Vector<EastWestSection> sections;

	/**
	 * Constructor.
	 * @param name			The name of the Road.
	 * @param weight		The weight of the road.
	 * @param maxVehicles 	The number of vehicles on the road.
	 */
	public EastWestRoad(String name, double distance, int maxVehicles, Metric metric) {
		super(name, distance, maxVehicles, metric, "East/West");
		eastBound = new Vector<Vehicle>();
		westBound = new Vector<Vehicle>();
		sections = new Vector<EastWestSection>();
	}

	/**
	 * Returns the number of vehicles on the road.
	 * @param direction	The direction the vehicle is traveling.
	 * @return	The number of vehicles on the road.
	 */
	public int getNumVehicles (Direction direction) {
		int numVehicles = -1;

		switch (direction) {
		case East:
			numVehicles = eastBound.size();
			break;
		case West:
			numVehicles = westBound.size();
			break;
		}

		return numVehicles;
	}
	
	/**
	 * @see Road#generateSections()
	 */
	public boolean generateSections() {
		Intersection[] intersectionArray = new Intersection[intersections.size()];
		String temp = "";
		intersections.toArray(intersectionArray);
		
		for (int i = 0; i < intersectionArray.length-1; i++) {
			temp = intersectionArray[i].getName() + " and " + intersectionArray[i+1].getName();
			EastWestSection tempSection = new EastWestSection(name, intersectionArray[i], intersectionArray[i+1], (distance/intersectionArray.length), maxVehicles, metric, temp);
			sections.addElement(tempSection);
			intersectionArray[i].addRoadSection(tempSection);
			intersectionArray[i+1].addRoadSection(tempSection);
		}
		
		return true;
	}
	
	/** 
	 * Prints the structure of this EastWestRoad (for debugging).
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		String print = super.toString();
	
		EastWestSection[] sectionArray = new EastWestSection[sections.size()];
		sections.toArray(sectionArray);
		
		print = print.concat("\n\t\t\t\tSections of Road: (" + sectionArray.length + " in total)\n\n");
		for (int i = 0; i < sectionArray.length; i++) {
			print = print.concat("\t\t\t\t\tBetween: " + sectionArray[i].getDescription() + "\n\n");
			print = print.concat("\t\t\t\t\t\tDistance: " + sectionArray[i].getDistance() + "\n");
			print = print.concat("\t\t\t\t\t\tEastBound Weight: " + sectionArray[i].getWeight(Direction.East) + "\n");
			print = print.concat("\t\t\t\t\t\tWestBound Weight: " + sectionArray[i].getWeight(Direction.West) + "\n\n");
		}
		
		return print;
	}
	
	/**
	 * @see Road#getSections()
	 */
	public Section[] getSections() {
		Section[] sectionArray = new Section[sections.size()];
		sections.toArray(sectionArray);
		return sectionArray;
	}
}